package top.zxybird.common.query;

/**
 * RangeQuery.
 * 
 * Created by zxybird on 2017/11/16.
 */
public class RangeQuery extends Query {
  protected String field;
  private String lower;
  private String upper;
  private boolean includeLower;
  private boolean includeUpper;

  public RangeQuery(String field, String lower, String upper) {
    this(field, lower, upper, true, false);
  }

  public RangeQuery(String field, String lower, String upper, boolean includeLower, boolean includeUpper) {
    this.includeLower = true;
    this.includeUpper = false;
    this.field = field;
    this.lower = lower;
    this.upper = upper;
    this.includeLower = includeLower;
    this.includeUpper = includeUpper;
  }

  public String getField() {
    return this.field;
  }

  public String getLower() {
    return this.lower;
  }

  public String getUpper() {
    return this.upper;
  }

  public boolean isIncludeLower() {
    return this.includeLower;
  }

  public boolean isIncludeUpper() {
    return this.includeUpper;
  }

  public String toString() {
    if (this.field == null) {
      return null;
    } else {
      StringBuilder stringBuilder = new StringBuilder(256);
      stringBuilder.append(this.field).append(":").append((char) (this.includeLower ? '[' : '{'))
          .append(this.lower == null ? "*" : this.escape(this.lower)).append(" TO ")
          .append(this.upper == null ? "*" : this.escape(this.upper)).append((char) (this.includeUpper ? ']' : '}'));
      return stringBuilder.toString();
    }
  }
}
